package com.plan.car.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.ObjectUtil;
import com.plan.car.entity.Dept;
import com.plan.car.entity.Role;
import com.plan.car.entity.User;
import com.plan.car.entity.dto.DeptDTO;
import com.plan.car.entity.dto.RoleDTO;
import com.plan.car.entity.vo.DeptVO;
import com.plan.car.entity.vo.RoleVO;
import com.plan.car.service.RoleService;
import com.plan.car.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;

    @GetMapping("/list")
    @ApiOperation("查询角色列表接口")
    public SaResult getRoleList(){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                List<Role> roleList = roleService.list();
                List<RoleVO> roleVOList = new ArrayList<>();
                if(!ObjectUtil.isEmpty(roleList)){
                    for(Role role:roleList){
                        RoleVO roleVO = new RoleVO();
                        roleVO.setRoleId(role.getRoleId());
                        roleVO.setRoleName(role.getRoleName());
                        roleVOList.add(roleVO);
                    }
                    return SaResult.data(roleVOList);
                }else{
                    return SaResult.error("角色列表为空");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }
    @ApiOperation("新增角色接口")
    @PostMapping("/add")
    public SaResult addRole(@RequestBody RoleDTO roleDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try{
                    Role role = new Role();
                    role.setRoleName(roleDTO.getRoleName());
                    role.setCreateTime(new Date());
                    role.setCreateBy(userId);
                    roleService.save(role);
                    return SaResult.ok("新增角色成功");
                }catch (Exception e){
                    return SaResult.error("新增角色失败");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("删除角色接口")
    @PostMapping("/delete")
    public SaResult deleteDept(@RequestBody RoleDTO roleDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try {
                    roleService.removeById(roleDTO.getRoleId());
                    return SaResult.ok("删除角色成功");
                }catch (Exception e){
                    return SaResult.error("删除角色失败");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("修改角色信息接口")
    @PostMapping("/edit")
    public SaResult editDept(@RequestBody RoleDTO roleDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try{
                    Role role = new Role();
                    role.setRoleId(roleDTO.getRoleId());
                    role.setRoleName(roleDTO.getRoleName());
                    role.setUpdateBy(userId);
                    role.setUpdateTime(new Date());
                    roleService.updateById(role);
                    return SaResult.ok("修改角色信息成功");
                }catch (Exception e){
                    return SaResult.error("角色信息更新失败");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("查询角色信息接口")
    @PostMapping("/select")
    public SaResult selectRole(@RequestBody RoleDTO roleDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try {
                    Role role = roleService.getById(roleDTO.getRoleId());
                    RoleVO roleVO = new RoleVO();
                    roleVO.setRoleId(role.getRoleId());
                    roleVO.setRoleName(role.getRoleName());
                    return SaResult.data(roleVO);
                }catch (NullPointerException e){
                    return SaResult.error("角色不存在");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }
}
